package com.kui.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.kui.entity.Qikan;
import com.kui.entity.Type;
import com.kui.entity.Wenxian;
import com.kui.service.QikanService;
import com.kui.service.TypeService;
import com.kui.service.WenxianService;

public class GaojiServlet extends HttpServlet {

	
	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WenxianService ws = new WenxianService();
		QikanService qs = new QikanService();
		TypeService ts = new TypeService();
		List<Wenxian> wenxianlist = ws.findAll();
		List<String> namelist = new ArrayList<String>();
		List<String> authorlist = new ArrayList<String>();
		
		List<String> idlist = new ArrayList<String>();
		
		
		List<Qikan> qikanlist = qs.findAll();
		List<Type> typelist = ts.findAll();
		
		List<String> qnlist = new ArrayList<String>();
		List<String> tnlist = new ArrayList<String>();
		
		for(Wenxian w : wenxianlist){
			namelist.add('"'+w.getName()+'"');
			
			String[] names = w.getAuthor().split("，");
			for (int i = 0; i < names.length; i++) {
				String name = names[i];
				authorlist.add('"'+name+'"');
			}
			
			String[] keywords = w.getKeyword().split("，");
			for (int i = 0; i < keywords.length; i++) {
				String keyword = keywords[i];
				namelist.add('"'+keyword+'"');
			}
			
			idlist.add('"'+w.getId()+'"');
		}
		
		for(Qikan q : qikanlist){
			qnlist.add('"'+q.getName()+'"');
			
		}
		
		for(Type t : typelist){
			tnlist.add('"'+t.getName()+'"');
		}
		//去除检索重复词
		//暂时省略不写，构造另一个list去除重复和精确查询类似
		List<String> namelist1 = new ArrayList<String>();
		List<String> authorlist1 = new ArrayList<String>();
		
		
		
		String temp = "";
		for(String names : namelist){
			temp = names;
			if(!(namelist1.contains(temp))){
				namelist1.add(temp);
			}
		}
		
		for(String names : authorlist){
			temp = names;
			if(!(authorlist1.contains(temp))){
				authorlist1.add(temp);
			}
		}
		
		
		
		
		request.setAttribute("namelist", namelist1);
		request.setAttribute("authorlist",authorlist1);
		request.setAttribute("idlist",idlist );
		request.setAttribute("qnlist",qnlist );
		request.setAttribute("tnlist", tnlist);
		
		request.getRequestDispatcher("WEB-INF/views/gaoji.jsp").forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String n2 = request.getParameter("n2");
		String name = request.getParameter("name");
		
		String a1 = request.getParameter("a1");
		String author = request.getParameter("author");
		String a2 = request.getParameter("a2");
		
		String q1 = request.getParameter("q1");
		String qikan = request.getParameter("qikan");
		String q2 = request.getParameter("q2");
		
		String t1 = request.getParameter("t1");
		String type = request.getParameter("type");
		String t2 = request.getParameter("t2");
		
		String i1 = request.getParameter("i1");
		String id = request.getParameter("id");
		String i2 = request.getParameter("i2");
		
		StringBuilder sb = new StringBuilder();
		String str1 = "";
		String str2 = "";
		String str3 = "";
		String str4 = "";
		String str5 = "";
		if(!("".equals(name))){
			
			if("mohu".equals(n2)){
				
				for (int i = 0; i < name.length(); i++) {
					str1 += name.substring(i,i+1) + "%" ;
				}
				str1=" (t_wenxian.name like "+ "'"+"%"+str1+"'"+" or t_wenxian.keyword like " + "'"+"%"+str1+"'"+")";
			}else{
				str1=" (t_wenxian.name like "+ "'"+"%"+name+"'"+" or t_wenxian.keyword like " + "'"+"%"+name+"'"+")";
			}
			
			
		}
		
		
		
		
		
		if(!("".equals(author))){
			
			if("mohu".equals(a2)){
				
				for (int i = 0; i < author.length(); i++) {
					str2 += author.substring(i,i+1) + "%" ;
				}
				str2 =" t_wenxian.author like "+"'"+"%"+str2 +"'";
			}else{
				str2 =" t_wenxian.author like "+"'"+"%"+author+"%"+"'";
			}
			
			
		}
		
		
		
		
		if(!("".equals(qikan))){
			
			if("mohu".equals(q2)){
				
				for (int i = 0; i < qikan.length(); i++) {
					str3 += qikan.substring(i,i+1) + "%" ;
				}
				str3=" t_qikan.name like "+"'"+"%"+str3+"'";
			}else{
				str3=" t_qikan.name like "+"'"+"%"+qikan+"%"+"'";
			}
			
			
		}
		
		
		
		
		if(!("".equals(type))){
			
			if("mohu".equals(t2)){
				
				for (int i = 0; i < type.length(); i++) {
					str4 += type.substring(i,i+1) + "%" ;
				}
				str4=" t_type.name like "+"'"+"%"+str4+"'";
			}else{
				str4=" t_type.name like "+"'"+"%"+type+"%"+"'";
			}
			
			
		}
		
		
		
		
		if(!("".equals(id))){
			
			if("mohu".equals(i2)){
				
				for (int i = 0; i < id.length(); i++) {
					str5 += id.substring(i,i+1) + "%" ;
				}
				str5=" t_wenxian.id like "+"'"+"%"+str5+"'";
			}else{
				str5=" t_wenxian.id like "+"'"+"%"+id+"%"+"'";
			}
		}
		
		if(!("".equals(name))){
			sb.append(str1);
		}
		if(!("".equals(author)) && "and".equals(a1)){
			sb.append(" and "+str2);
		}
		if(!("".equals(qikan)) && "and".equals(q1)){
			sb.append(" and "+str3);
		}
		if(!("".equals(type)) && "and".equals(t1)){
			sb.append(" and "+str4);
		}
		if(!("".equals(id)) && "and".equals(i1)){
			sb.append(" and "+str5);
		}
		
		if(!("".equals(author)) && "or".equals(a1)){
			sb.append(" or "+str2);
		}
		if(!("".equals(qikan)) && "or".equals(q1)){
			sb.append(" or "+str3);
		}
		if(!("".equals(type)) && "or".equals(t1)){
			sb.append(" or "+str4);
		}
		if(!("".equals(id)) && "or".equals(i1)){
			sb.append(" or "+str5);
		}
		String s = "";
		if("".equals(name)){
			
			s = sb.substring(4);
		}
		System.out.println(sb);
		System.out.println(s);
		WenxianService ws = new WenxianService();
		List<Wenxian> wenxianlist = new ArrayList<Wenxian>();
		if("".equals(s)){
			wenxianlist = ws.findLike(sb.toString());
		}else{
			wenxianlist = ws.findLike(s);
		}
		
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		
			
			
			Gson gson = new Gson();
			String json = gson.toJson(wenxianlist);
			out.print(json);
	
		

		
		out.flush();
		out.close();
	}

}
